package com.ts.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ts.content.entity.DiscussEntity;
import com.ts.content.mapper.DiscussMapper;
import com.ts.content.service.DiscussService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 话题
 *
 * @author admin admin@qq.com
 * @since 1.0.0 2023-02-22
 */
@Service
public class DiscussServiceImpl extends ServiceImpl<DiscussMapper, DiscussEntity> implements DiscussService {

    @Override
    public void hasDiscussList(Integer userId, List<String> discussList) {

        QueryWrapper<DiscussEntity> queryWrapper = new QueryWrapper();
        queryWrapper.in("title",discussList);

        //在数据中查询是否有此标题。
        List<DiscussEntity> discussEntities = baseMapper.selectList(queryWrapper);

        //如果在话题表中没有就加进去
        if (ObjectUtils.isEmpty(discussEntities)){
            DiscussEntity entity = new DiscussEntity();
            entity.setCreateTime(new Date());
            for (String title : discussList) {
                entity.setTitle(title);
                entity.setUserId(userId);
                baseMapper.insert(entity);
            }
        }else {
            for (DiscussEntity discussEntity : discussEntities) {
                //如果有就加帖子数量
                Integer postNum = discussEntity.getPostNum();
                discussEntity.setPostNum(++postNum);
                baseMapper.update(discussEntity,queryWrapper);
            }
        }


    }

    @Override
    public List<DiscussEntity> getHotDiscussList() {
        return baseMapper.getHotDiscussList();
    }
}